package com.societyGames.codeAlong.state.codeExercise.view
{
import com.societyGames.flashForms.FrameContainer;
import com.societyGames.flashForms.VerticalScrollContainer;
import com.societyGames.flashForms.basicSkin.BasicControls;
import com.societyGames.flashForms.basicSkin.SharedTextFields;

import flash.text.TextField;
import flash.text.TextFieldAutoSize;

public class HyperTextToolTip extends ToolTip
	{
		private var _maxWidth:Number;
		private var _maxHeight:Number;
		private var _textField:TextField;
		private var _verticalScrollContainer:VerticalScrollContainer;
		private var _frameContainer:FrameContainer;		
		
		public function HyperTextToolTip()
		{
			this._textField = SharedTextFields.createHtmlTextField();
			this._textField.autoSize = TextFieldAutoSize.LEFT;
			this._textField.multiline = true;
			this._textField.wordWrap = true;
			
			this._verticalScrollContainer = BasicControls.createVerticalScrollContainer();
			this._verticalScrollContainer.item = this._textField;

			this._frameContainer = BasicControls.createToolTipFrameContainer();
			this._frameContainer.item = this._verticalScrollContainer;

			this.item = this._frameContainer;
		}
		
		public function set maximumWidth(value:Number):void
		{
			if (value != this._maxWidth)
			{
				this._maxWidth = value;
				recalculate();
			}
		}
		
		public function get maximumWidth():Number
		{
			return this._maxWidth;
		}
		
		public function set maximumHeight(value:Number):void
		{
			if (value != this._maxHeight)
			{
				this._maxHeight = value;
				recalculate();
			}
		}
		
		public function get maximumHeight():Number
		{
			return this._maxWidth;
		}
		
		public function set hyperText(value:String):void
		{
			this._textField.htmlText = value;
			recalculate();
		}
		
		public function get hyperText():String
		{
			return this._textField.htmlText;
		}
		
		private function recalculate():void
		{
			if (this._textField.height > this._maxHeight)
			{
				this._textField.width = this._maxWidth - this._verticalScrollContainer.verticalTrackWidth;
				this._verticalScrollContainer.width = this._maxWidth;
				this._verticalScrollContainer.height = this._maxHeight;
				this._frameContainer.innerWidth = this._maxWidth;
				this._frameContainer.innerHeight = this._maxHeight;
			}
			else
			{
				this._textField.width = this._maxWidth;
				this._verticalScrollContainer.width = this._maxWidth;
				this._verticalScrollContainer.height = this._textField.height;
				this._frameContainer.innerWidth = this._maxWidth;
				this._frameContainer.innerHeight = this._textField.height;
			}
		}
	}
}